package demo2;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import org.junit.Test;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @description: 数据库连接池C3P0的测试
 * @author: apple
 * @date:  2022/1/29 9:51 上午
 * @version 1.0
 **/
public class C3P0Test {


    // 方式一
    @Test
    public void testConnection() throws PropertyVetoException, SQLException {
        // 获取数据库连接池
        ComboPooledDataSource cpds = new ComboPooledDataSource();

//        DataSource d = cpds;

        cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
        cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true" );
        cpds.setUser("root");
        cpds.setPassword("123456");
        // 初始数据库连接池的连接数量
        cpds.setInitialPoolSize(10);
        final Connection connection = cpds.getConnection();

        System.out.println(connection);

        System.out.println(connection.getClass());



        cpds.close();

        // 销毁连接池
//        DataSources.destroy(cpds);

    }

    // 方式二：使用配置文件
    @Test
    public void testConnection2() throws SQLException {
        ComboPooledDataSource cpds = new ComboPooledDataSource("intergalactoApp");

        final Connection connection = cpds.getConnection();

        System.out.println(connection.getClass());

        cpds.close();

    }

    @Test
    public void testConnection3() throws SQLException {
        final Connection connection = JDBCUtils.getConnection();

        System.out.println(connection);
    }
}
